/**
 * @author K老叶(Klaoye)
 * @license Apache 2.0 阅读器界面
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class ReaderFrame extends JFrame {

	JLabel file_path_label = new JLabel();// 文件路径标签
	JTextArea text_area = new JTextArea();// 文本域
	JSlider font_size_slider = new JSlider(15, 50, 15);// 字体大小拖动条
	JToggleButton catalog_visible_toggleButton = new JToggleButton("显示目录(L)");// 显示目录开关
	Dialog dialog_process = new MyDialog(this).wait("加载中", "正在加载文件");
	JMenuBar menu_bar = new JMenuBar();
	JPanel s_panel = new JPanel();// 南面板
	JList<String> catalog_list = new JList<>();// 目录列表
	JScrollPane w_panel = new JScrollPane(catalog_list);
	JScrollPane c_panel = new JScrollPane(text_area);// 中面板


	Model model = new Model(this);// 模型
	Controller control = new Controller(model, this);// 控制器

	public ReaderFrame() {
		init();
	}

	private void init() {
		Container container = new Container();// 容器
		JMenu file_menu = new JMenu("文件(F)");
		JMenuItem open_file_item = new JMenuItem("打开");
		JMenu tools_menu = new JMenu("工具(T)");
		JMenu book_mark_manage = new JMenu("书签管理");
		JMenuItem add_book_mark = new JMenuItem("添加书签");
		JMenuItem read_book_mark = new JMenuItem("读取书签");
		JMenuItem clear_book_mark = new JMenuItem("清空书签");
		JMenu settings_menu = new JMenu("设置(S)");
		JMenu color_style_menu = new JMenu("颜色模式");

		catalog_list.setVisible(false);
		catalog_list.setFont(new Font("宋体", Font.PLAIN, font_size_slider.getValue()));
		catalog_list.setCellRenderer(Tools.listCellRenderer);
		//catalog_list.setSize(this.getWidth()/4, getHeight());
		catalog_visible_toggleButton.setMnemonic(KeyEvent.VK_L);
		catalog_visible_toggleButton.setDisplayedMnemonicIndex(5);

		w_panel.setHorizontalScrollBar(new JScrollBar());
		w_panel.setVerticalScrollBar(new JScrollBar());
		w_panel.setSize(this.getWidth() / 4, this.getHeight());
		w_panel.setVisible(false);

		text_area.setLineWrap(true);
		text_area.setWrapStyleWord(true);
		text_area.setEditable(false);
		text_area.getCaret().setVisible(true);
		text_area.setFont(new Font("宋体", Font.PLAIN, 15));

		font_size_slider.setMajorTickSpacing(10);
		font_size_slider.setMinorTickSpacing(10);
		font_size_slider.setPaintLabels(true);
		font_size_slider.setPaintTicks(true);
		font_size_slider.setToolTipText("字体大小");

		s_panel.add(font_size_slider);
		s_panel.add(catalog_visible_toggleButton);
		s_panel.add(file_path_label);

		open_file_item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK));

		file_menu.setMnemonic(KeyEvent.VK_F);
		file_menu.setDisplayedMnemonicIndex(3);
		file_menu.add(open_file_item);
		settings_menu.add(color_style_menu);
		settings_menu.setMnemonic(KeyEvent.VK_S);
		tools_menu.setMnemonic(KeyEvent.VK_T);
		tools_menu.add(book_mark_manage);
		book_mark_manage.add(add_book_mark);
		book_mark_manage.add(read_book_mark);
		book_mark_manage.add(clear_book_mark);

		open_file_item.addActionListener(control.changeFile(this.model));
		catalog_visible_toggleButton.addChangeListener(control.catalogChangeListener());
		add_book_mark.addActionListener(control.addMarkListener());
		read_book_mark.addActionListener(control.manageMarkListener());
		clear_book_mark.addActionListener(control.clearMarkListener());
		font_size_slider.addChangeListener(e -> {
			text_area.setFont(new Font("宋体", Font.PLAIN, font_size_slider.getValue()));
			catalog_list.setFont(new Font("宋体", Font.PLAIN, font_size_slider.getValue()));
		});
		for (ColorStyle style : ColorStyle.values()) {
			JMenuItem style_item = new JMenuItem(style.name());
			style_item.addActionListener(control.colorStyleListener(style));
			color_style_menu.add(style_item);
		}

		menu_bar.add(file_menu);
		menu_bar.add(tools_menu);
		menu_bar.add(settings_menu);

		container.setLayout(new BorderLayout());
		container.add(s_panel, BorderLayout.SOUTH);
		container.add(w_panel, BorderLayout.WEST);
		container.add(c_panel, BorderLayout.CENTER);
		container.add(menu_bar, BorderLayout.NORTH);

		add(container);
		setTitle("小说阅读器");
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		addWindowListener(control.windowListener());
	}
}
